package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dto.UserDto;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = "INSERT INTO `tab_user` (`username`, `password`) " +
                "VALUES ('" + userModel.getUsername() + "'," +
                " '" + userModel.getPassword() + "');";

        return JDBC.excuteUpdate(sql);
    }

    @Override
    public UserModel findByUsernameIsHave(UserModel userModel) {
        String sql = "select * from tab_user where username='" + userModel.getUsername() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
//                model.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public UserModel findByUsernameAndPasswordIsHave(UserModel userModel) {
        String sql = "select * from tab_user where " +
                "username='" + userModel.getUsername() + "' " +
                "and password='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;//没有值
        try {
            while (resultSet.next()) {
                model = new UserModel();//有值，但是值为空
//                model.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public List<UserModel> findAll(UserDto userDto, PageModel pageModel) {
        String sql = "select * from tab_user  where 1=1 ";
        if (!"".equals(userDto.getUserModel().getUsername())) {
            sql += " and username like '%" + userDto.getUserModel().getUsername() + "%' ";
        }
        if (!"".equals(userDto.getUserModel().getPassword())) {
            sql += " and password like '%" + userDto.getUserModel().getPassword() + "%' ";
        }
        if (userDto.getUserModel().getSex() == 1 || userDto.getUserModel().getSex() == 0) {
            sql += " and sex=" + userDto.getUserModel().getSex();
        }

        sql += " and age between " + userDto.getAgeStart() + " and " + userDto.getAgeEnd();

        sql += " limit  " + (pageModel.getPageNum() - 1) * pageModel.getPageSize() + "  ," + pageModel.getPageSize();
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel userModel = new UserModel();
                userModel.setId(resultSet.getInt("id"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPassword(resultSet.getString("password"));
                userModel.setAge(resultSet.getInt("age"));
                userModel.setSex(resultSet.getInt("sex"));
                list.add(userModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public int del(int id) {
        String sql = "delete from tab_user where id=" + id;
        int result = 0;
        try {
            result = JDBC.excuteUpdate(sql);
        } finally {
            JDBC.close();
        }
        return result;
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "update tab_user " +
                "set password='" + userModel.getPassword() + "' " +
                "where id=" + userModel.getId();
        int result = 0;
        try {
            result = JDBC.excuteUpdate(sql);
        } finally {
            JDBC.close();
        }
        return result;
    }

    @Override
    public UserModel findByUserId(int id) {
        String sql = "select * from tab_user where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = new UserModel();
                userModel.setId(resultSet.getInt("id"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPassword(resultSet.getString("password"));
                userModel.setAge(resultSet.getInt("age"));
                userModel.setSex(resultSet.getInt("sex"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public int count(UserDto userDto) {
        String sql = "select count(id) as count from tab_user  where 1=1 ";
        if (!"".equals(userDto.getUserModel().getUsername())) {
            sql += " and username like '%" + userDto.getUserModel().getUsername() + "%' ";
        }
        if (!"".equals(userDto.getUserModel().getPassword())) {
            sql += " and password like '%" + userDto.getUserModel().getPassword() + "%' ";
        }
        if (userDto.getUserModel().getSex() == 1 || userDto.getUserModel().getSex() == 0) {
            sql += " and sex=" + userDto.getUserModel().getSex();
        }
        sql += " and age between " + userDto.getAgeStart() + " and " + userDto.getAgeEnd();
        ResultSet resultSet = JDBC.getResultSet(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
